package cn.edu.xmu.orderpayment.payment.Controller;

import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.orderpayment.payment.Service.ReconciliationService;
import cn.edu.xmu.privilegegateway.annotation.aop.Audit;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginName;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;



import java.time.LocalDateTime;
import java.time.ZonedDateTime;

import static cn.edu.xmu.oomall.core.util.Common.decorateReturnObject;

@Api(value = "支付", tags = "payment")
@RestController
@RequestMapping(value = "/", produces = "application/json;charset=UTF-8")
public class ReconciliationController {

    @Autowired
    private ReconciliationService reconciliationService;

    /**
     *
     * @param loginId
     * @param loginName
     * @param beginTime
     * @param endTime
     * @return
     * @author wang xusheng
     */
    @ApiOperation(value = "对账")
    @Audit
    @GetMapping("/shops/{id}/reconciliation")
    public Object reconciliation(@PathVariable Long id,@LoginUser Long loginId,@LoginName String loginName,
                                 @RequestParam(value = "beginTime", required = false) @DateTimeFormat(pattern = "uuuu-MM-dd'T'HH:mm:ss.SSSXXX") ZonedDateTime beginTime,
                                 @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "uuuu-MM-dd'T'HH:mm:ss.SSSXXX") ZonedDateTime endTime){
        if (0!=id){
            ReturnObject ret = new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE,"非管理员不能操作");//不是0，说明不是管理员，返回505错误
            return decorateReturnObject(ret);
        }
        //参数时间合法性检查
        if(beginTime.isAfter(endTime)){
            ReturnObject ret = new ReturnObject(ReturnNo.LATE_BEGINTIME);
            return decorateReturnObject(ret);
        }
        ReturnObject res = reconciliationService.reconciliation(loginId,loginName,beginTime,endTime);
        return decorateReturnObject(res);
    }

}
